package com.dou.sample.opengl_project.shader;

import android.content.Context;

import com.dou.sample.opengl_project.R;

import static android.opengl.GLES20.GL_TEXTURE0;
import static android.opengl.GLES20.GL_TEXTURE_2D;
import static android.opengl.GLES20.glActiveTexture;
import static android.opengl.GLES20.glBindTexture;
import static android.opengl.GLES20.glGetAttribLocation;
import static android.opengl.GLES20.glGetUniformLocation;
import static android.opengl.GLES20.glUniform1i;
import static android.opengl.GLES20.glUniformMatrix4fv;

/**
 * Created by Dou on 2018/1/16.
 */

public class TextureShaderProgram extends ShaderProgram {

    int aPositionLocation;
    int aTextureCoodinatesLocation;
    int uMatrixLocation;
    int uTextureUnitLocation;

    public TextureShaderProgram(Context context) {
        super(context, R.raw.texture_vertex_shader, R.raw.texture_fragment_shader);

        aPositionLocation = glGetAttribLocation(program, A_POSITION);
        aTextureCoodinatesLocation = glGetAttribLocation(program, A_TEXTURECOODINATES);
        uMatrixLocation = glGetUniformLocation(program, U_MATRIX);
        uTextureUnitLocation = glGetUniformLocation(program, U_TEXTUREUNIT);
    }

    public void setUniform(float[] matrix, int textureId){
        glUniformMatrix4fv(uMatrixLocation, 1, false, matrix, 0);

        glActiveTexture(GL_TEXTURE0);

        glBindTexture(GL_TEXTURE_2D, textureId);

        glUniform1i(uTextureUnitLocation, 0);
    }

    public int getaPositionLocation(){
        return aPositionLocation;
    }

    public int getaTextureCoodinatesLocation(){
        return aTextureCoodinatesLocation;
    }
}
